import fileio from '@ohos.fileio';
import featureAbility from '@ohos.ability.featureAbility';

let path: string = null

const init = () =>{
    const context = featureAbility.getContext();
    return context.getFilesDir().then((data) => {
        path = data;
    })
}

const write = (data: string, fileName?: string) => {
    fileName = fileName || "data.data"
    const fd = fileio.openSync(`${path}/${fileName}`, 0o100 | 0o2, 0o666);
    fileio.writeSync(fd, data)
}

const reader = (fileName?: string) => {
    fileName = fileName || "data.data"
    try {
        console.log(`${path}/${fileName}`)
        return fileio.readTextSync(`${path}/${fileName}`)
    }
    catch(err) {
        return false
    }
}


export default {
    init, write, reader, path
}
